package com.huoyun.car.action;

import java.util.List;
import java.text.ParseException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.huoyun.base.action.BaseAction;
import com.huoyun.car.bean.Car;
import com.huoyun.car.bean.CarPubInfo;
import com.huoyun.car.bean.CarRoute;
import com.huoyun.car.service.CarPubInfoService;
import com.huoyun.car.service.CarRouteService;
import com.huoyun.car.service.CarService;
import com.huoyun.user.action.UserAction;

/**
 * 与"发布车源"相关的action
 * 
 * @author ChengXin
 */
public class CarPubInfoAction extends BaseAction {

	private static final long serialVersionUID = 1L;
	private static final Log log = LogFactory.getLog(UserAction.class);

	private Long id;

	/**
	 * jsp前端读入的是与车牌号相关的id
	 */
	private int carDetailID;

	/**
	 * "车源信息"中的默认联系方式
	 */
	private String defaultContact;

	/**
	 * 一个"车源信息"实体
	 */
	private CarPubInfo carPubInfo;

	/**
	 * 管理"车源信息"的service
	 */
	private CarPubInfoService carPubInfoService;

	/**
	 * 管理"车辆信息"的service
	 */
	private CarService carService;

	/**
	 * 车辆列表
	 */
	private List<Car> carList;

	/**
	 * 某一条车源信息的起始地信息(最多三组起始地)
	 */
	private CarRoute carRouteA;
	private CarRoute carRouteB;
	private CarRoute carRouteC;

	/**
	 * 管理"起始地、到达地信息"的service
	 */
	private CarRouteService carRouteService;

	/**
	 * 车辆的最大可在吨位
	 */
	private String loadMax;

	/**
	 * 发布一条"车源信息"
	 * 
	 * @return SUCCESS
	 * @throws ParseException
	 */
	public String publishCarInfo() throws ParseException {

		// 添加一条"车源信息"
		carPubInfo.setUserId(getLoginUser().getId()); // 设置用户id
		carPubInfo.setStatus(1); // 设置车源信息状态为"上线"
		// carPubInfo.setRetour(2); //设置是否为返程空车

		if (carPubInfo.getType() == 0)	//整车运输
		{
			carPubInfo.setLoadAmount(carService.getCarById(carPubInfo.getCarId()).getLoadMax());
			carPubInfo.setUnit("3193");
			carPubInfo.setOtherInfo(null);
		}
		
		carPubInfoService.publishCarInfo(carPubInfo);

		// 添加一条"车源信息"的起始地信息(最少一组,最多三组起始地)
		Long carPubInfoId = carPubInfo.getId();
		this.carRouteA.setCarInfoId(carPubInfoId);
		this.carRouteA.setType(1);
		this.carRouteService.saveCarRoute(this.carRouteA);

		if (carRouteB != null
				&& carRouteB.getStartProvince().compareTo("") != 0
				&& !carRouteB.getStartProvince().startsWith(",")
				&& carRouteB.getEndProvince().compareTo("") != 0
				&& !carRouteB.getEndProvince().startsWith(",")) {
			this.carRouteB.setCarInfoId(carPubInfoId);
			this.carRouteB.setType(0);
			this.carRouteService.saveCarRoute(this.carRouteB);
		}
		if (carRouteC != null
				&& carRouteC.getStartProvince().compareTo("") != 0
				&& !carRouteC.getStartProvince().startsWith(",")
				&& carRouteC.getEndProvince().compareTo("") != 0
				&& !carRouteC.getEndProvince().startsWith(",")) {
			this.carRouteC.setCarInfoId(carPubInfoId);
			this.carRouteC.setType(0);
			this.carRouteService.saveCarRoute(this.carRouteC);
		}

		return SUCCESS;
	}

	/**
	 * 跳转到"修改车源信息"页面前的准备工作
	 * 
	 * @return SUCCESS
	 */
	public String redirectEditPublishedCarInfo() {

		carPubInfo = carPubInfoService.getCarPubInfoById(id);

		List<CarRoute> carRouteList = this.carRouteService
				.getCarRouteListByCarPubInfoId(id);

		switch (carRouteList.size()) {
		case 3:
			this.carRouteA = carRouteList.get(0);
			this.carRouteB = carRouteList.get(1);
			this.carRouteC = carRouteList.get(2);
			break;
		case 2:
			this.carRouteA = carRouteList.get(0);
			this.carRouteB = carRouteList.get(1);
			break;
		case 1:
			this.carRouteA = carRouteList.get(0);
			break;
		default:
			this.carRouteA = carRouteList.get(0);
		}

		carList = carService.getUnPublishedCarListByUserId(getLoginUser()
				.getId());
		carList.add(carService.getCarById(carPubInfo.getCarId()));

		this.loadMax = carService.getCarById(carPubInfo.getCarId())
				.getLoadMax();

		return SUCCESS;
	}

	/**
	 * 更新一条"车源信息"
	 * 
	 * @return SUCCESS
	 */
	public String updateCarInfo() {

		// 更新一条"车源信息"
		carPubInfo.setUserId(getLoginUser().getId());
		carPubInfo.setStatus(0);
		this.id = Long.parseLong(getRequest().getParameter("carPubInfoId"));
		carPubInfo.setId(this.id);
		carPubInfo.setStatus(1);

		if (carPubInfo.getType() == 0)
		{
			carPubInfo.setLoadAmount(carService.getCarById(carPubInfo.getCarId()).getLoadMax());
			carPubInfo.setUnit("3193");
			carPubInfo.setOtherInfo(null);
		}
		
		carPubInfoService.updateAll(carPubInfo);

		// 更新当前车源的所有起始地和到达地信息(最少一对,最多三对起始地和到达地信息)
		// 路线1
		long carRouteAId = Long.parseLong(getRequest().getParameter(
				"carRouteAId"));
		this.carRouteA.setId(carRouteAId);
		this.carRouteA.setCarInfoId(this.id);
		this.carRouteA.setType(1);
		this.carRouteService.updateCarRoute(this.carRouteA);
		// 路线2
		if (carRouteB != null
				&& carRouteB.getStartProvince().compareTo("") != 0
				&& !carRouteB.getStartProvince().startsWith(",")
				&& carRouteB.getEndProvince().compareTo("") != 0
				&& !carRouteB.getEndProvince().startsWith(",")) {
			if (getRequest().getParameter("carRouteBId").compareTo("") != 0) {
				long carRouteBId = Long.parseLong(getRequest().getParameter(
						"carRouteBId"));
				this.carRouteB.setId(carRouteBId);
				this.carRouteB.setCarInfoId(this.id);
				this.carRouteB.setType(0);
				this.carRouteService.updateCarRoute(carRouteB);
			} else {
				this.carRouteB.setCarInfoId(this.id);
				this.carRouteB.setType(0);
				this.carRouteService.saveCarRoute(this.carRouteB);
			}
		} else if (getRequest().getParameter("carRouteBId").compareTo("") != 0) {
			long carRouteBId = Long.parseLong(getRequest().getParameter(
					"carRouteBId"));
			this.carRouteService.deleteCarRoute(carRouteBId);
		}
		// 路线3
		if (carRouteC != null
				&& carRouteC.getStartProvince().compareTo("") != 0
				&& !carRouteC.getStartProvince().startsWith(",")
				&& carRouteC.getEndProvince().compareTo("") != 0
				&& !carRouteC.getEndProvince().startsWith(",")) {
			if (getRequest().getParameter("carRouteCId").compareTo("") != 0) {
				long carRouteCId = Long.parseLong(getRequest().getParameter(
						"carRouteCId"));
				this.carRouteC.setId(carRouteCId);
				this.carRouteC.setCarInfoId(this.id);
				this.carRouteC.setType(0);
				this.carRouteService.updateCarRoute(carRouteC);
			} else {
				this.carRouteC.setCarInfoId(this.id);
				this.carRouteC.setType(0);
				this.carRouteService.saveCarRoute(this.carRouteC);
			}
		} else if (getRequest().getParameter("carRouteCId").compareTo("") != 0) {
			long carRouteCId = Long.parseLong(getRequest().getParameter(
					"carRouteCId"));
			this.carRouteService.deleteCarRoute(carRouteCId);
		}

		return SUCCESS;
	}

	/**
	 * 更新一条"车源信息"的状态：0,未上线; 1,在线
	 * 
	 * @return
	 */
	public String updateCarPubInfoStatus() {
		int status = Integer.parseInt(getRequest().getParameter("status"));
		long carPubInfoId = Long.parseLong(getRequest().getParameter(
				"carPubInfoId"));

		this.carPubInfoService.updateCarPubInfoStatus(carPubInfoId, status);

		return SUCCESS;
	}

	public List<Car> getCarList() {
		return carList;
	}

	public void setCarList(List<Car> carList) {
		this.carList = carList;
	}

	public CarService getCarService() {
		return carService;
	}

	public void setCarService(CarService carService) {
		this.carService = carService;
	}

	public CarPubInfo getCarPubInfo() {
		return carPubInfo;
	}

	public void setCarPubInfo(CarPubInfo carPubInfo) {
		this.carPubInfo = carPubInfo;
	}

	public CarPubInfoService getCarPubInfoService() {
		return carPubInfoService;
	}

	public void setCarPubInfoService(CarPubInfoService carPubInfoService) {
		this.carPubInfoService = carPubInfoService;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public int getCarDetailID() {
		return carDetailID;
	}

	public void setCarDetailID(int carDetailID) {
		this.carDetailID = carDetailID;
	}

	public String getDefaultContact() {
		return defaultContact;
	}

	public void setDefaultContact(String defaultContact) {
		this.defaultContact = defaultContact;
	}

	public CarRoute getCarRouteA() {
		return carRouteA;
	}

	public void setCarRouteA(CarRoute carRouteA) {
		this.carRouteA = carRouteA;
	}

	public CarRoute getCarRouteB() {
		return carRouteB;
	}

	public void setCarRouteB(CarRoute carRouteB) {
		this.carRouteB = carRouteB;
	}

	public CarRoute getCarRouteC() {
		return carRouteC;
	}

	public void setCarRouteC(CarRoute carRouteC) {
		this.carRouteC = carRouteC;
	}

	public CarRouteService getCarRouteService() {
		return carRouteService;
	}

	public void setCarRouteService(CarRouteService carRouteService) {
		this.carRouteService = carRouteService;
	}

	public String getLoadMax() {
		return loadMax;
	}

	public void setLoadMax(String loadMax) {
		this.loadMax = loadMax;
	}

}
